package com.cskaoyan.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskaoyan.mall.product.converter.dto.PlatformAttributeInfoConverter;
import com.cskaoyan.mall.product.converter.param.PlatformAttributeInfoParamConverter;
import com.cskaoyan.mall.product.dto.PlatformAttributeInfoDTO;
import com.cskaoyan.mall.product.dto.PlatformAttributeValueDTO;
import com.cskaoyan.mall.product.mapper.PlatformAttrInfoMapper;
import com.cskaoyan.mall.product.mapper.PlatformAttrValueMapper;
import com.cskaoyan.mall.product.model.PlatformAttributeInfo;
import com.cskaoyan.mall.product.model.PlatformAttributeValue;
import com.cskaoyan.mall.product.query.PlatformAttributeParam;
import com.cskaoyan.mall.product.service.PlatformAttributeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class PlatformAttributeServiceImpl implements PlatformAttributeService {
    @Autowired
    PlatformAttrInfoMapper platformAttrInfoMapper;

    @Autowired
    PlatformAttributeInfoConverter platformAttributeInfoConverter;

    @Autowired
    PlatformAttrValueMapper platformAttrValueMapper;

    @Autowired
    PlatformAttributeInfoParamConverter platformAttributeInfoParamConverter;

    @Override
    public List<PlatformAttributeInfoDTO> getPlatformAttrInfoList(Long firstLevelCategoryId, Long secondLevelCategoryId, Long thirdLevelCategoryId) {
        List<PlatformAttributeInfo> platformAttributeInfoList = platformAttrInfoMapper.selectPlatFormAttrInfoList(firstLevelCategoryId, secondLevelCategoryId, thirdLevelCategoryId);
        List<PlatformAttributeInfoDTO> platformAttributeInfoDTOList = platformAttributeInfoConverter.platformAttributeInfoPOs2DTOs(platformAttributeInfoList);
        return platformAttributeInfoDTOList;
    }

    @Override
    public void savePlatformAttrInfo(PlatformAttributeParam platformAttributeParam) {
        PlatformAttributeInfo platformAttributeInfo=platformAttributeInfoParamConverter.attributeInfoParam2Info(platformAttributeParam);
        if(platformAttributeInfo.getId() !=null){
            platformAttrInfoMapper.updateById(platformAttributeInfo);
        }else {
            platformAttrInfoMapper.insert(platformAttributeInfo);
        }
        LambdaQueryWrapper<PlatformAttributeValue> platformAttributeValueLambdaQueryWrapper=new LambdaQueryWrapper<>();
        platformAttributeValueLambdaQueryWrapper.eq(PlatformAttributeValue::getAttrId,platformAttributeInfo.getId());
        platformAttrValueMapper.delete(platformAttributeValueLambdaQueryWrapper);

        List<PlatformAttributeValue> attributeValueList=platformAttributeInfo.getAttrValueList();
        if(!CollectionUtils.isEmpty(attributeValueList)){
            for (PlatformAttributeValue attributeValue:attributeValueList) {
                attributeValue.setId(null);
                attributeValue.setAttrId(platformAttributeInfo.getId());
                platformAttrValueMapper.insert(attributeValue);
            }
        }
    }

    @Override
    public List<PlatformAttributeValueDTO> getPlatformAttrInfo(Long attrId) {
        QueryWrapper<PlatformAttributeValue> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("attr_id",attrId);
        List<PlatformAttributeValue> platformAttributeValuesList =platformAttrValueMapper.selectList(queryWrapper);
        List<PlatformAttributeValueDTO> platformAttributeValueDTOList=platformAttributeInfoConverter.platformAttributeValuePOs2DTOs(platformAttributeValuesList);
        return platformAttributeValueDTOList;
    }
}
